package vistas;
import clientermi.conexionRMI;
import java.rmi.RemoteException;


public class vistaPrincipal extends javax.swing.JFrame {
    private conexionRMI conexion = new conexionRMI();
    java.util.Date fecha = new java.util.Date();
    private String nombre = String.valueOf(fecha.getTime());

    public vistaPrincipal() {
        initComponents();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonIngresar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPaneLog = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Servidor Poblacional");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Registro"));

        jButtonIngresar.setText("Registrar Servidor");
        jButtonIngresar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                ingresar(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jButtonIngresar)
                .addContainerGap(263, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jButtonIngresar)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jScrollPane1.setEnabled(false);

        jTextPaneLog.setEditable(false);
        jTextPaneLog.setText("Servidor Pablacional Activo...\n");
        jScrollPane1.setViewportView(jTextPaneLog);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ingresar(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_ingresar
        //accion de para registrar el servidor poblacional al servidor maestro
        if (jButtonIngresar.getText().equals("Registrar Servidor")) {

            jTextPaneLog.setText(jTextPaneLog.getText()+"Intentando registrar servidor poblacional."+"\n");
            try {

                //si el boton esta en modo de registro
                jTextPaneLog.setText(jTextPaneLog.getText()+"Verificando Conexión con el seridor maestro."+"\n");

                if (conexion.iniciarRegistry()) {

                    //si tenemos conexion con el servidor
                    jTextPaneLog.setText(jTextPaneLog.getText()+"Conexión establecida."+"\n");

                    if (conexion.getServidor().inicioSesion(nombre)) {

                        //servidor registrado
                        jTextPaneLog.setText(jTextPaneLog.getText()+"Servidor registrado."+"\n");
                    }
                    else {

                        //imposible registrar servidor
                        jTextPaneLog.setText(jTextPaneLog.getText()+"Imposible registrar servidor."+"\n");
                    }

                } else {

                    //no se puede encontrar la conexion
                    jTextPaneLog.setText(jTextPaneLog.getText()+"Conexión no encontrada."+"\n");

                }
            } catch (RemoteException ex) {

                //error no controlado
                jTextPaneLog.setText(jTextPaneLog.getText()+"Error al registrar servidor."+"\n");
            }
        }


        
        
}//GEN-LAST:event_ingresar
 
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonIngresar;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPaneLog;
    // End of variables declaration//GEN-END:variables

}
